/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.controller.front;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mxg.blog.common.CqLogger;
import com.mxg.blog.common.ResultCode;
import com.mxg.blog.entity.Project;
import com.mxg.blog.entity.Skill;
import com.mxg.blog.entity.User;
import com.mxg.blog.service.ProjectService;
import com.mxg.blog.service.SkillService;
import com.mxg.blog.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/7/22 23:49
 * 获取我的个人信息
 */
@Controller
public class FrontGetUser {
    CqLogger logger = CqLogger.getInstance();

    private final SkillService skillService;
    private final UserService userService;
    private final ProjectService projectService;

    public FrontGetUser(SkillService skillService,
                        UserService userService,
                        ProjectService projectService) {
        this.skillService = skillService;
        this.userService = userService;
        this.projectService = projectService;
    }

    @GetMapping("api/getUser")
    public void getUser(HttpServletResponse response) throws Exception {
        User user = userService.selectUserById(1);

        Map<String, Object> tableData = new HashMap<>();

        tableData.put(ResultCode.nickName, user.getUserName());
        tableData.put(ResultCode.email, user.getEmail());
        tableData.put(ResultCode.phone, user.getPhone());

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(tableData);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    @GetMapping("api/getSkills")
    public void getSkills(HttpServletResponse response)throws Exception{
        List<Map<String, Object>> tableData = new ArrayList<>();

        List<Skill> skillList = skillService.getSkillsFront(1);

        for(Skill skill : skillList){
            Map<String, Object> skillInfo = new HashMap<>();

            skillInfo.put(ResultCode.id, skill.getSkillsId());
            skillInfo.put(ResultCode.content, skill.getSkillsContent());

            tableData.add(skillInfo);
        }

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(tableData);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    @GetMapping("api/getProjects")
    public void getProjects(HttpServletResponse response)throws Exception{
        List<Map<String, Object>> tableData = new ArrayList<>();

        List<Project> projectList = projectService.getProjects(1);

        for(Project project : projectList){
            Map<String, Object> skillInfo = new HashMap<>();

            skillInfo.put(ResultCode.id, project.getProjectId());
            skillInfo.put(ResultCode.title, project.getProjectName());
            skillInfo.put(ResultCode.content, project.getProjectContent());
            skillInfo.put(ResultCode.harvest, project.getProjectHarvest());
            skillInfo.put(ResultCode.address, project.getProjectAddress());
            skillInfo.put(ResultCode.startDate, project.getProjectBeginTime());
            skillInfo.put(ResultCode.endDate, project.getProjectEndTime());

            tableData.add(skillInfo);
        }

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(tableData);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }
}
